<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class MusicsController extends AppController {

    var $name = 'Musics';
    var $components = array(
        'Session', 'Alias', 'Upload', 'Resize', 'Crop'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array(
        'Music'
    );

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - Nhạc');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Music.id' => 'DESC'
            ),
        );

        $data = $this->paginate('Music');
        $this->set('total', $this->params['paging']['Music']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Music->alias}.{$this->Music->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );

        //delete images
        $data = $this->Music->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Music.id', 'Music.thumbnail')
                        )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('music_thumb_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('music_thumb_absolute_path') . $image_file);
            }
        }

        //delete media file
        $data = $this->Music->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Music.id', 'Music.url')
                        )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('music_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('music_absolute_path') . $image_file);
            }
        }
        if ($this->Music->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d nhạc", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->Music->writeHomeMusics();
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Music->id = $params['cid'][0];
        } else {
            $this->Music->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }

        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');

        if (empty($this->data)) {
            $fields = array(
                '*'
            );

            $this->data = $this->Music->read($fields);
            $this->set('singer_data', $this->Music->Singer->getListAll());
            $this->set('style_data', $this->Music->music_style->getListAll());
            $check_thumbnail_exist = is_file(Configure::read('music_thumb_absolute_path') . $this->data['Music']['thumbnail']);
            $this->set('check_thumbnail_exist', $check_thumbnail_exist);
            $this->set('image_url', Configure::read('music_thumb_url_path') . $this->data['Music']['thumbnail']);
            $this->set('description', $this->data['Music']['description']);

            $check_flash_exist = is_file(Configure::read('music_absolute_path') . $this->data['Music']['url']);
            $this->set('check_flash_exist', $check_flash_exist);
            $this->set('video_url', Configure::read('music_url_path') . $this->data['Music']['url']);
        }
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm nhạc');
        $this->set('singer_data', $this->Music->Singer->getListAll());
        $this->set('style_data', $this->Music->music_style->getListAll());
    }

    function crop() {
        $form = $this->params['form'];
        $img_130_130_filename = Configure::read('music_thumb_absolute_path') . $form['img_130_130_filename'];
        if ($this->Crop->loadImage($img_130_130_filename)) {
            $this->Crop->cropToDimensions($form['img_130_130_sx'], $form['img_130_130_sy'], $form['img_130_130_ex'], $form['img_130_130_ey']);
            $this->Crop->saveImage($img_130_130_filename);
            $this->Crop->flushImages();
        }
        $this->Session->setFlash('đã crop ảnh thành công', 'msg', array(), 'msg');
        $this->redirect(array('action' => 'index'));
    }

    function save() {
        $data = $this->data['Music'];

        //for upload thumbnail
        $this->Upload->setUploadpath(Configure::read('music_thumb_absolute_path'));
        $this->Upload->setRules('size', Configure::read('content_image_maxsize'));
        $this->Upload->setRules('ext', Configure::read('content_image_ext'));
        $row = $this->Music->findById($this->data['Music']['id']);

        //upload small thumbnail
        if ($this->data['Music']['thumbnail']['name'] != '') {
            if ($this->Upload->validate($this->data['Music']['thumbnail'])) {
                $data['thumbnail'] = $this->Upload->upload($this->data['Music']['thumbnail']);
                $this->Resize->run_resize(Configure::read('music_thumb_absolute_path') . $data['thumbnail'], 130, 130);

                //delete it
                if (is_file(Configure::read('music_thumb_absolute_path') . $row['Music']['thumbnail']) && $row['Music']['thumbnail'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('music_thumb_absolute_path') . $row['Music']['thumbnail']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'musics', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['thumbnail']);
            //delete image
            if (!empty($this->data['Music']['delete_image'])) {

                if (is_file(Configure::read('music_thumb_absolute_path') . $row['Music']['thumbnail']) && $row['Music']['thumbnail'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('music_thumb_absolute_path') . $row['Music']['thumbnail']);
                    $data['thumbnail'] = 'no_image.gif';
                }
            }
        }
        //end upload thumbnail
        //upload flash
        if ($this->data['Music']['url']['name'] != '') {
            $this->Upload->setUploadpath(Configure::read('music_absolute_path'));
            if ($this->Upload->validate($this->data['Music']['url'])) {
                $data['url'] = $this->Upload->upload($this->data['Music']['url']);

                //delete it
                if (is_file(Configure::read('music_absolute_path') . $row['Music']['url']) && $row['Music']['url'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('music_absolute_path') . $row['Music']['url']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'musics', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['url']);
        }

        $data['alias'] = $this->Alias->alias($data['title']);
        if ($this->Music->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Music->invalidFields(), 'error', array(), 'msg');
        }
        $this->Music->writeHomeMusics();
        $this->redirect(array('controller' => 'musics', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'musics', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->Music->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Music->invalidFields(), 'error', array(), 'msg');
        }
        $this->Music->writeHomeMusics();
        $this->redirect(array('controller' => 'musics', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->Music->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Music->invalidFields(), 'error', array(), 'msg');
        }
        $this->Music->writeHomeMusics();
        $this->redirect(array('controller' => 'musics', 'action' => 'index'));
    }

}

?>
